package com.seeyon.apps.dzdd.manager.impl;

import com.seeyon.apps.dzdd.kit.FormCap4Kit;
import com.seeyon.apps.dzdd.kit.StrKit;
import com.seeyon.apps.dzdd.manager.AssetsInventoryManager;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.cap4.template.manager.CAPDataListSqlHelpManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.common.po.affair.CtpAffair;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.DBAgent;
import org.apache.commons.logging.Log;

import java.text.SimpleDateFormat;
import java.util.*;

public class AssetsInventoryManagerImpl implements AssetsInventoryManager {

    private static final Log log = CtpLogFactory.getLog(AssetsInventoryManagerImpl.class);

    private String bmzcpd_unFlowformCode = AppContext.getSystemProperty("dzdd.bmzcpd_unFlowformCode");

    private OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
    private EnumManager enumManager = (EnumManager) AppContext.getBean("enumManagerNew");
    private CAP4FormManager cap4FormManager = (CAP4FormManager) AppContext.getBean("cap4FormManager");
    private CAPDataListSqlHelpManager capDataListSqlHelpManager = (CAPDataListSqlHelpManager) AppContext.getBean("capDataListSqlHelpManager");

    /**
     * 获取资产盘点计划列表
     *
     * @param param 参数映射，可能包含成员ID等信息
     * @return 资产盘点计划的列表，每个计划信息作为Map存储
     * @throws BusinessException 如果查询过程中遇到业务异常
     */
    @Override
    public List<Map<String, Object>> getAssetsInventoryplanList(Map<String, Object> param) throws BusinessException {
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        try {
            // 获取资产盘点计划的表单代码配置
            String flowformCode = AppContext.getSystemProperty("dzdd.flowformCode");
            // 根据表单代码获取表单对象
            FormBean formBean = cap4FormManager.getFormByFormCode(flowformCode);
            // 如果表单对象为空，记录日志并返回空列表
            if (formBean == null) {
                log.info("资产盘点计划表未设置表单模板编码或表单编码维护错误:" + flowformCode);
                return dataList;
            }
            String planCode = "";
            if (param.containsKey("planCode")) {
                planCode = StrKit.str(param.get("planCode"));
            }

            // 提取参数中的成员ID，使用工具类进行字符串处理
            String memberId = StrKit.str(param.get("memberId"));
            // 获取主表名
            String mainTableName = formBean.getMasterTableBean().getTableName();
            // 通过显示名称获取字段名，用于SQL查询
            String zhdhFieldName = formBean.getFieldBeanByDisplay("盘点工作编号").getName();
            String pdrFieldName = formBean.getFieldBeanByDisplay("盘点人").getName();
            String subTableName = formBean.getFieldBeanByDisplay("盘点人").getOwnerTableName();
            String zcbmFieldName = formBean.getFieldBeanByDisplay("资产部门").getName();
            String jprFieldName = formBean.getFieldBeanByDisplay("监盘人").getName();
            // 构建SQL查询字符串
            StringBuilder stb = new StringBuilder();
            stb.append("SELECT f.id,f1.").append(zhdhFieldName).append(",f1.").append(pdrFieldName).append(",f1.").append(jprFieldName);
            stb.append(",f1.").append(zcbmFieldName).append(" FROM ").append(mainTableName).append(" f ");
            stb.append("LEFT JOIN ").append(subTableName).append(" f1 ON f.id = f1.formmain_id ");
            stb.append("WHERE ").append("f1.").append(pdrFieldName).append(" LIKE '%").append(memberId).append("%'");
            stb.append(" AND f.finishedflag = '1'");
            if (!"".equals(planCode)) {
                stb.append(" AND f1.").append(zhdhFieldName).append(" LIKE '%").append(planCode).append("%'");
            }
            // 记录查询SQL的日志信息
            log.info("根据判断人查询资产盘点计划信息sql语句:" + stb.toString());
            // 执行SQL查询并获取结果列表
            List list = capDataListSqlHelpManager.selectDataList(stb.toString(), null);
            // 如果结果为空，直接返回空列表
            if (list.isEmpty()) {
                return dataList;
            }
            // 遍历查询结果，处理并封装数据
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> newDataMap = new HashMap<String, Object>();
                Map<String, Object> dataMap = (Map<String, Object>) list.get(i);
                // 提取主键ID
                String mainId = StrKit.str(dataMap.get("id"));
                newDataMap.put("mainId", mainId);
                // 盘点计划单号
                String zcpdjhdh = StrKit.str(dataMap.get(zhdhFieldName));
                newDataMap.put("planCode", zcpdjhdh);
                // 处理资产部门信息
                String zcbmId = StrKit.str(dataMap.get(zcbmFieldName));
                Map<String, Object> zcbmMap = new HashMap<String, Object>();
                if (!"".equals(zcbmId)) {
                    zcbmMap.put("deptId", zcbmId);
                    String deptName = orgManager.getDepartmentById(Long.parseLong(zcbmId)).getName();
                    zcbmMap.put("deptName", deptName);
                }
                newDataMap.put("dept", zcbmMap);
                // 处理盘点人信息
                String pdrId = StrKit.str(dataMap.get(pdrFieldName));
                log.info("盘点人信息:" + pdrId);
                String new_pdrId = pdrId.replaceAll("\\[", "").replaceAll("]", "");
                String[] pdrIds = new_pdrId.split(",");
                List<Map<String, Object>> pdrList = new ArrayList<Map<String, Object>>();
                for (int j = 0; j < pdrIds.length; j++) {
                    Map<String, Object> pdrMap = new HashMap<String, Object>();
                    String pdrIdStr = pdrIds[j];
                    pdrMap.put("memberId", pdrIdStr);
                    String pdrName = orgManager.getMemberById(Long.parseLong(pdrIdStr)).getName();
                    pdrMap.put("memberName", pdrName);
                    pdrList.add(pdrMap);
                }
                newDataMap.put("member", pdrList);
                String jprId = StrKit.str(dataMap.get(jprFieldName));
                Map<String, Object> jprMap = new HashMap<String, Object>();
                if (!"".equals(jprId)) {
                    jprMap.put("assessorId", jprId);
                    String jprName = orgManager.getMemberById(Long.parseLong(jprId)).getName();
                    jprMap.put("assessorName", jprName);
                }
                newDataMap.put("assessor", jprMap);
                // 根据状态和表单记录ID查询事务信息
                CtpAffair ctpAffair = getAffairsByStateAndformRecordIdSql(Long.parseLong(mainId));
                // 构建事务处理详情的URL
                String url = "/seeyon/collaboration/collaboration.do?method=summary&openFrom=listSent&affairId=" + ctpAffair.getId() + "&dumpData=undefined&showTab=true";
                // 将构建好的URL添加到新数据映射中
                newDataMap.put("url", url);
                dataList.add(newDataMap);
            }
        } catch (Exception e) {
            // 记录异常日志信息
            log.error("查询资产盘点计划方法异常信息:", e);
        }
        // 返回最终的数据列表
        return dataList;
    }


    /**
     * 添加或更新资产盘点信息
     * <p>
     * 此方法主要用于根据提供的参数更新或插入资产盘点数据它首先根据资产编码查询资产信息，
     * 然后根据查询结果更新资产盘点档案和资产盘点情况表最后，返回一个包含操作结果和消息的地图
     *
     * @param param 包含资产信息的参数地图，用于更新或插入资产盘点数据
     * @return 包含操作结果和消息的Map，结果类型为'S'表示成功，'E'表示错误
     * @throws BusinessException 如果在处理业务逻辑时发生异常，则抛出此业务异常
     */
    @Override
    public Map<String, Object> addOrUpdateAssetsInventory(Map<String, Object> param) throws BusinessException {
        Map<String, Object> assetDataMap = new HashMap<String, Object>();
        try {
            // 添加或更新资产盘点情况表数据，并记录操作结果
            boolean bmzcpd_addOrUpdate = addbmzcpdAssetsData(param);
            log.info("添加或更新资产盘点情况表标识:" + bmzcpd_addOrUpdate);
            // 根据操作结果，构建返回地图
            if (bmzcpd_addOrUpdate == true) {
                assetDataMap.put("type", "S");
                assetDataMap.put("msg", "资产盘点成功");
            } else {
                assetDataMap.put("type", "E");
                assetDataMap.put("msg", "资产盘点失败");
            }
        } catch (Exception e) {
            // 异常处理，记录错误信息并构建返回地图
            assetDataMap.put("type", "E");
            assetDataMap.put("msg", "资产盘点添加或更新失败:" + e.getMessage());
            log.error("添加资产盘点功能异常信息:", e);
        }
        return assetDataMap;
    }

    /**
     * 根据资产编码查询资产信息
     *
     * @param assetsCode 资产编码
     * @return 包含资产信息的结果映射
     * @throws BusinessException 如果查询过程中发生业务异常
     */
    @Override
    public Map<String, Object> getAssetsByAssetsCode(String assetsCode, String deptId, String planNumber) throws BusinessException {
        // 初始化用于存储资产信息的Map
        Map<String, Object> dataMap_new = new HashMap<String, Object>();
        // 初始化用于返回的结果映射
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            boolean isInventory = checkAssetsInventory(assetsCode, planNumber);
            String zckp_unFlowformCode = "";
            // 获取系统属性中指定的未流转表单代码
            if (isInventory) {
                zckp_unFlowformCode = AppContext.getSystemProperty("dzdd.bmzcpd_unFlowformCode");
            } else {
                zckp_unFlowformCode = AppContext.getSystemProperty("dzdd.zckp_unFlowformCode");
            }
            // 根据表单代码获取表单对象
            FormBean zckp_formBean = cap4FormManager.getFormByFormCode(zckp_unFlowformCode);
            // 获取主表名称
            String zckp_tableName = StrKit.str(zckp_formBean.getMasterTableBean().getTableName());
            // 获取使用部门字段名
            String sybm_FieldName = "";
            String bz_FieldName = "";
            if (isInventory) {
                sybm_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("责任部门").getName());
                bz_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("备注").getName());
            } else {
                sybm_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("使用部门-W").getName());
            }
            // 获取资产编码字段名
            String zcbm_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("资产编码").getName());
            // 获取资产名称字段名
            String zcmc_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("资产名称").getName());
            // 获取资产状态字段名
            String zczt_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("资产状态").getName());
            // 获取资产类别字段名
            String zcnb_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("资产类别").getName());
            // 获取存放位置字段名
            String cfwz_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("存放位置").getName());
            // 获取使用保管人字段名
            String syr_FieldName = "";
            if (isInventory) {
                syr_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("使用保管人").getName());
            } else {
                syr_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("使用保管人-W").getName());
            }
            // 获取EAS编码字段名
            String easbm_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("EAS编码").getName());
            // 初始化StringBuilder用于构建SQL查询语句
            StringBuilder zckp_stb = new StringBuilder();
            // 构建SQL查询语句，选择需要的字段
            zckp_stb.append("SELECT id,").append(zcbm_FieldName).append(",");
            if (isInventory) {
                zckp_stb.append(bz_FieldName).append(",");
            }
            zckp_stb.append(zcmc_FieldName).append(",").append(zczt_FieldName).append(",");
            zckp_stb.append(zcnb_FieldName).append(",").append(cfwz_FieldName).append(",");
            zckp_stb.append(easbm_FieldName).append(",").append(syr_FieldName).append(",").append(sybm_FieldName).append(" FROM ");
            zckp_stb.append(zckp_tableName).append(" WHERE ").append(zcbm_FieldName).append(" ='").append(assetsCode).append("'");
            // 记录SQL查询语句日志
            log.info("根据资产编码查询资产卡片档案数据sql语句:" + zckp_stb.toString());
            // 执行SQL查询，获取资产数据列表
            List<Object> dataList = capDataListSqlHelpManager.selectDataList(zckp_stb.toString(), null);
            // 如果查询到数据，处理并填充dataMap_new
            if (!dataList.isEmpty()) {
                for (int i = 0; i < dataList.size(); i++) {
                    Map<String, Object> dataMap = (Map<String, Object>) dataList.get(i);
                    dataMap_new.put("assetsCode", dataMap.get(zcbm_FieldName));
                    dataMap_new.put("assetsName", dataMap.get(zcmc_FieldName));
                    dataMap_new.put("assetsType", StrKit.str(dataMap.get(zcnb_FieldName)));
                    dataMap_new.put("assetsLocation", StrKit.str(dataMap.get(cfwz_FieldName)));
                    String assetsUsePersonId = StrKit.str(dataMap.get(syr_FieldName));
                    String assetsUsePerson = "";
                    if (!"".equals(assetsUsePersonId)) {
                        assetsUsePerson = orgManager.getMemberById(Long.parseLong(assetsUsePersonId)).getName();
                    }
                    Map<String, Object> memberMap = new HashMap<String, Object>();
                    memberMap.put("id", assetsUsePersonId);
                    memberMap.put("name", assetsUsePerson);
                    dataMap_new.put("assetsUsePerson", memberMap);
                    dataMap_new.put("assetsStatus", StrKit.str(dataMap.get(zczt_FieldName)));
                    String assetsDeptId = StrKit.str(dataMap.get(sybm_FieldName));
                    String assetsDept = "";
                    if (!"".equals(assetsDeptId)) {
                        assetsDept = orgManager.getDepartmentById(Long.parseLong(assetsDeptId)).getName();
                    }
                    Map<String, Object> deptMap = new HashMap<String, Object>();
                    if (deptId.equals(assetsDeptId)) {
                        deptMap.put("isDept", true);
                    } else {
                        deptMap.put("isDept", false);
                    }
                    deptMap.put("id", assetsDeptId);
                    deptMap.put("name", assetsDept);
                    dataMap_new.put("assetsDept", deptMap);
                    String remark = "";
                    if (isInventory) {
                        remark = StrKit.str(dataMap.get(bz_FieldName));
                    } else {
                        remark = "";
                    }
                    dataMap_new.put("remark", remark);
                    dataMap_new.put("isInventory", isInventory);
                    dataMap_new.put("easCode", StrKit.str(dataMap.get(easbm_FieldName)));
                    dataMap_new.put("assetsId", StrKit.str(dataMap.get("id")));
                }
                // 设置成功标志和消息
                resultMap.put("type", "S");
                resultMap.put("msg", "");
                resultMap.put("data", dataMap_new);
            } else {
                // 设置失败标志和消息，未查询到数据
                resultMap.put("type", "E");
                resultMap.put("msg", "未查询到资产卡片数据");
                resultMap.put("data", dataMap_new);
            }
        } catch (Exception e) {
            // 记录异常信息
            log.error("根据资产编码查询资产卡片档案表异常信息:", e);
            // 设置失败标志和消息
            resultMap.put("type", "E");
            resultMap.put("msg", e.getMessage());
            resultMap.put("data", dataMap_new);
        }
        // 返回结果映射
        return resultMap;
    }

    /**
     * 根据部门获取资产数据列表
     * 本方法通过部门ID查询该部门下的所有资产信息，包括资产编码、名称、状态、类别、存放位置、使用人等
     *
     * @param param 参数映射，包含资产部门ID
     * @return 包含资产信息的Map列表
     * @throws BusinessException 当查询过程中遇到异常情况时抛出业务异常
     */
    @Override
    public Object getDeptAssetsDatasList(Map<String, Object> param, boolean isDetails) throws BusinessException {
        List<Map<String, Object>> assetDataList = new ArrayList<Map<String, Object>>();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            String planNumber = StrKit.str(param.get("planNumber"));
            // 获取部门ID
            String deptId = StrKit.str(param.get("assetsDeptId"));
            // 获取资产卡片未流转表单代码配置项
            String zckp_unFlowformCode = AppContext.getSystemProperty("dzdd.zckp_unFlowformCode");
            // 通过表单代码获取表单对象
            FormBean zckp_formBean = cap4FormManager.getFormByFormCode(zckp_unFlowformCode);
            // 获取资产卡片表名
            String zckp_tableName = zckp_formBean.getMasterTableBean().getTableName();
            // 获取使用部门字段名
            String sybm_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("使用部门-W").getName());
            // 获取资产编码字段名
            String zcbm_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("资产编码").getName());
            // 获取资产名称字段名
            String zcmc_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("资产名称").getName());
            // 获取资产状态字段名
            String zczt_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("资产状态").getName());
            // 获取资产类别字段名
            String zcnb_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("资产类别").getName());
            // 获取存放位置字段名
            String cfwz_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("存放位置").getName());
            // 获取使用保管人字段名
            String syr_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("使用保管人-W").getName());
            // 构建查询资产卡片SQL
            StringBuilder zckp_stb = new StringBuilder();
            zckp_stb.append("SELECT id,").append(zcbm_FieldName).append(",");
            zckp_stb.append(zcmc_FieldName).append(",").append(zczt_FieldName).append(",");
            zckp_stb.append(zcnb_FieldName).append(",").append(cfwz_FieldName).append(",");
            zckp_stb.append(syr_FieldName).append(",").append(sybm_FieldName).append(" FROM ");
            zckp_stb.append(zckp_tableName).append(" WHERE ").append(sybm_FieldName).append(" ='").append(deptId).append("'");
            /*资产状态不等于已处置*/
//            zckp_stb.append(" AND ").append(zczt_FieldName).append(" != '-553751124527921093'");
            /*客户环境*/
            zckp_stb.append(" AND ").append(zczt_FieldName).append(" != '7320494931582338298'");
            if (param.containsKey("assets")) {
                String assets = StrKit.str(param.get("assets"));
                if(!"".equals(assets)){
                    zckp_stb.append(" AND (").append(zcbm_FieldName).append(" like '%").append(assets).append("%'");
                    zckp_stb.append(" OR ").append(zcmc_FieldName).append(" like '%").append(assets).append("%')");
                }
            }
            log.info("根据部门查询资产卡片档案数据sql语句:" + zckp_stb.toString());
            // 执行SQL查询，获取资产数据列表
            List<Object> dataList = capDataListSqlHelpManager.selectDataList(zckp_stb.toString(), null);
            // 如果数据列表不为空，则处理每条资产数据
            if (!dataList.isEmpty()) {
                resultMap.put("sumNumber", dataList.size());
                int ypdNumber = 0;
                int wpdNumber = 0;
                for (int i = 0; i < dataList.size(); i++) {
                    Map<String, Object> dataMap_new = new HashMap<String, Object>();
                    Map<String, Object> dataMap = (Map<String, Object>) dataList.get(i);
                    String assetsCode = StrKit.str(dataMap.get(zcbm_FieldName));
                    dataMap_new.put("assetsCode", assetsCode);
                    dataMap_new.put("assetsName", dataMap.get(zcmc_FieldName));
                    String assetsTypeId = StrKit.str(dataMap.get(zcnb_FieldName));
                    String assetsType = "";
                    if (!"".equals(assetsTypeId)) {
                        assetsType = enumManager.getCtpEnumItem(Long.parseLong(assetsTypeId)).getShowvalue();
                    }
                    dataMap_new.put("assetsType", assetsType);
                    String assetsLocationId = StrKit.str(dataMap.get(cfwz_FieldName));
                    String assetsLocation = "";
                    if (!"".equals(assetsLocationId)) {
                        assetsLocation = enumManager.getCtpEnumItem(Long.parseLong(assetsLocationId)).getShowvalue();
                    }
                    dataMap_new.put("assetsLocation", assetsLocation);
                    String assetsUsePersonId = StrKit.str(dataMap.get(syr_FieldName));
                    String assetsUsePerson = "";
                    if (!"".equals(assetsUsePersonId)) {
                        assetsUsePerson = orgManager.getMemberById(Long.parseLong(assetsUsePersonId)).getName();
                    }
                    dataMap_new.put("assetsUsePerson", assetsUsePerson);
                    String assetsStatusId = StrKit.str(dataMap.get(zczt_FieldName));
                    String assetsStatus = "";
                    if (!"".equals(assetsStatusId)) {
                        assetsStatus = enumManager.getCtpEnumItem(Long.parseLong(assetsStatusId)).getShowvalue();
                    }
                    dataMap_new.put("assetsStatus", assetsStatus);
                    String assetsDeptId = StrKit.str(dataMap.get(sybm_FieldName));
                    String assetsDept = "";
                    if (!"".equals(assetsDeptId)) {
                        assetsDept = orgManager.getDepartmentById(Long.parseLong(assetsDeptId)).getName();
                    }
                    dataMap_new.put("assetsDept", assetsDept);
                    dataMap_new.put("assetsId", StrKit.str(dataMap.get("id")));
                    /*是否已盘点*/
                    boolean flag = checkAssetsInventory(assetsCode, planNumber);
                    if (flag) {
                        ypdNumber += 1;
                        resultMap.put("wpdNumber", wpdNumber);
                        resultMap.put("ypdNumber", ypdNumber);
                        dataMap_new.put("isInventory", "是");
                    } else {
                        wpdNumber += 1;
                        resultMap.put("ypdNumber", ypdNumber);
                        resultMap.put("wpdNumber", wpdNumber);
                        dataMap_new.put("isInventory", "否");
                    }
                    assetDataList.add(dataMap_new);
                }
            }
        } catch (Exception e) {
            log.error("根据资产部门查询资产数据异常信息:", e);
        }
        if (isDetails) {
            return assetDataList;
        } else {
            return resultMap;
        }
    }


    /**
     * 根据状态和表单记录ID查询事务信息
     * 此方法专用于从数据库中检索与特定状态和表单记录ID相关的事务信息
     * 注意：当前实现仅返回查询结果中的第一条记录，这可能意味着假设查询结果至少有一条记录
     *
     * @param formRecordId 表单记录的唯一标识符，用于定位具体的表单记录
     * @return CtpAffair对象，包含匹配的事务信息
     * @throws BusinessException 当数据库查询操作失败或查询结果为空时抛出
     */
    private CtpAffair getAffairsByStateAndformRecordIdSql(long formRecordId) throws BusinessException {
        // 定义查询SQL，筛选出符合特定表单记录ID和状态的事务
        String sql = "from CtpAffair as affair where affair.formRecordid = :formRecordid and affair.state = :state ";
        // 创建参数映射，用于存放SQL中的参数键值对
        Map map = new HashMap();
        map.put("formRecordid", formRecordId);
        map.put("state", 2);
        // 执行数据库查询，并转换结果列表，此处假设查询至少会返回一条记录
        // 注意：仅获取查询结果的第一个元素，可能存在只取一条数据的特定业务逻辑
        return (CtpAffair) DBAgent.find(sql, map).get(0);
    }

    /**
     * 检查资产盘点表中是否存在指定的资产编码
     *
     * @param assetsCode 资产编码
     * @return 如果资产编码存在于资产盘点表中，则返回true；否则返回false
     * @throws BusinessException 当查询操作失败时抛出业务异常
     */
    private boolean checkAssetsInventory(String assetsCode, String planNumber) throws BusinessException {
        Boolean flag = false;
        try {
            // 获取非流动资产盘点表的表单代码，用于查询表单信息
            String zcpd_unFlowformCode = AppContext.getSystemProperty("dzdd.bmzcpd_unFlowformCode");
            // 通过表单代码获取表单对象
            FormBean zcpd_formBean = cap4FormManager.getFormByFormCode(zcpd_unFlowformCode);
            // 通过显示名称获取字段对象，以获取字段名
            String zcbmFieldName = zcpd_formBean.getFieldBeanByDisplay("资产编码").getName();
            String pdgzbhFieldName = zcpd_formBean.getFieldBeanByDisplay("盘点工作编号").getName();
            String sfpdFieldName = zcpd_formBean.getFieldBeanByDisplay("是否盘到").getName();

            // 获取主表的表名
            String tableName = zcpd_formBean.getMasterTableBean().getTableName();
            // 构建SQL查询语句，用于查询资产编码是否存在与指定的表中
            StringBuilder stb = new StringBuilder();
            stb.append("SELECT id FROM ").append(tableName).append(" WHERE ").append(zcbmFieldName).append(" = '").append(assetsCode).append("'");
            stb.append(" AND ").append(pdgzbhFieldName).append(" = '").append(planNumber).append("'").append(" AND ").append(sfpdFieldName).append(" = '-2541467559084539290'");
            // 执行SQL查询，并获取结果列表
            List<Object> dataList = capDataListSqlHelpManager.selectDataList(stb.toString(), null);
            // 如果结果列表不为空，则表示找到了对应的资产编码，设置flag为true
            if (!dataList.isEmpty()) {
                flag = true;
            }
        } catch (Exception e) {
            // 记录异常信息，用于排查问题
            log.error("根据资产编码查询资产盘点档案表数据异常信息:", e);
        }
        // 返回检查结果
        return flag;
    }


    /**
     * 私有方法，用于添加或更新资产盘点数据
     *
     * @param param 参数映射，包含资产的相关信息
     * @return 操作是否成功的布尔值
     * @throws BusinessException 如果业务逻辑处理失败则抛出此异常
     */
    private boolean addbmzcpdAssetsData(Map<String, Object> param) throws BusinessException {
        try {
            String planNumber = StrKit.str(param.get("planNumber"));
            log.info("盘点计划编号：" + planNumber);
            FormBean bmzcpd_formBean = cap4FormManager.getFormByFormCode(bmzcpd_unFlowformCode);
            String zcpd_tableName = bmzcpd_formBean.getMasterTableBean().getTableName();
            String zcpd_zcbm_fieldName = bmzcpd_formBean.getFieldBeanByDisplay("资产编码").getName();
            String zcpd_pdgzbh_fieldName = bmzcpd_formBean.getFieldBeanByDisplay("盘点工作编号").getName();
            // 构建SQL查询语句，用于查询指定资产编码的资产盘点数据
            StringBuilder stb = new StringBuilder();
            stb.append("SELECT id,").append(zcpd_pdgzbh_fieldName).append(" FROM ").append(zcpd_tableName).append(" WHERE ").append(zcpd_zcbm_fieldName).append(" = '").append(param.get("assetsCode")).append("'");
            stb.append(" AND ").append(zcpd_pdgzbh_fieldName).append(" = '").append(planNumber).append("'");
            log.info("查询资产编码查询部门资产盘点档案数据SQL语句：" + stb.toString());
            // 执行SQL查询并获取数据列表
            List<Object> dataList = capDataListSqlHelpManager.selectDataList(stb.toString(), null);
            // 初始化表单主数据对象和数据ID变量
            FormDataMasterBean bmzcpd_formDataMasterBean = null;
            String zcpd_dataId = "";
            // 如果查询到数据，获取第一条数据的ID
            if (!dataList.isEmpty()) {
                for (int i = 0; i < dataList.size(); i++) {
                    Map<String, Object> dataMap = (Map<String, Object>) dataList.get(i);
                    zcpd_dataId = StrKit.str(dataMap.get("id"));
                }
            }
            // 根据查询结果，要么获取现有数据，要么创建新数据
            if (!"".equals(zcpd_dataId)) {
                bmzcpd_formDataMasterBean = cap4FormManager.getDataMasterBeanById(Long.parseLong(zcpd_dataId), bmzcpd_formBean, null);
            } else {
                bmzcpd_formDataMasterBean = FormDataMasterBean.newInstance(bmzcpd_formBean);
            }
            String sfgxzckp = StrKit.str(bmzcpd_formDataMasterBean.getFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("是否更新资产卡片").getName()));
            if ("".equals(sfgxzckp)) {
//                bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("是否更新资产卡片").getName(), -2048201178869905430L);
                /*客户环境*/
                bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("是否更新资产卡片").getName(), -5262438096267164683L);
            }
            // 向表单数据对象中添加或更新字段值
            if (planNumber.contains("-")) {
                String flowformCode = AppContext.getSystemProperty("dzdd.flowformCode");
                FormBean jhxd_formBean = cap4FormManager.getFormByFormCode(flowformCode);
                String planNumber_fieldName = jhxd_formBean.getFieldBeanByDisplay("盘点工作编号").getName();
                String jpr_fieldName = jhxd_formBean.getFieldBeanByDisplay("监盘人").getName();
                String jhxd_tableName = jhxd_formBean.getFieldBeanByDisplay("监盘人").getOwnerTableName();
                StringBuilder jhxdStb = new StringBuilder();
                jhxdStb.append("SELECT id,formmain_id,").append(jpr_fieldName).append(" FROM ").append(jhxd_tableName);
                jhxdStb.append(" WHERE ").append(planNumber_fieldName).append(" = '").append(planNumber).append("'");
                log.info("根据盘点计划编号查询资产盘点计划表数据SQL语句：" + jhxdStb.toString());
                // 执行SQL查询并获取数据列表
                List<Object> jhxd_dataList = capDataListSqlHelpManager.selectDataList(jhxdStb.toString(), null);
                if (!jhxd_dataList.isEmpty()) {
                    Map<String, Object> jhxd_dataMap = (Map<String, Object>) jhxd_dataList.get(0);
                    String jpr_id = StrKit.str(jhxd_dataMap.get(jpr_fieldName));
                    bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("监盘人").getName(), jpr_id);
                    String dataId = StrKit.str(jhxd_dataMap.get("formmain_id"));
                    FormDataMasterBean formDataMasterBean = cap4FormManager.getDataMasterBeanById(Long.parseLong(dataId), jhxd_formBean, null);
                    Object jhxd_date = formDataMasterBean.getFieldValue(jhxd_formBean.getFieldBeanByDisplay("计划下达日期").getName());
                    if (!"".equals(StrKit.str(jhxd_date))) {
                        String jhxd_dateStr = new SimpleDateFormat("yyyy-MM-dd").format((Date) jhxd_date);
                        bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("计划下达日期").getName(), jhxd_dateStr);
                    }
                    String jhr_id = StrKit.str(formDataMasterBean.getFieldValue(jhxd_formBean.getFieldBeanByDisplay("计划人").getName()));
                    bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("盘点计划下达人").getName(), jhr_id);
                }
            }

            bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("盘点工作编号").getName(), planNumber);
            bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("资产名称").getName(), param.get("assetsName"));
            bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("资产编码").getName(), param.get("assetsCode"));

            String zckp_unFlowformCode = AppContext.getSystemProperty("dzdd.zckp_unFlowformCode");
            FormBean zckp_formBean = cap4FormManager.getFormByFormCode(zckp_unFlowformCode);
            String zckp_tableName = zckp_formBean.getMasterTableBean().getTableName();
            String zcbm_zckp_fieldName = zckp_formBean.getFieldBeanByDisplay("资产编码").getName();
            String ggxh_zckp_fieldName = zckp_formBean.getFieldBeanByDisplay("规格型号").getName();
            String rkrr_zckp_fieldName = zckp_formBean.getFieldBeanByDisplay("入库日期").getName();
            String grjg_zckp_fieldName = zckp_formBean.getFieldBeanByDisplay("购入价格").getName();
            // 构建SQL查询语句，用于查询指定资产编码的资产盘点数据
            StringBuilder zckp_stb = new StringBuilder();
            zckp_stb.append("SELECT id,").append(rkrr_zckp_fieldName).append(",").append(ggxh_zckp_fieldName).append(",").append(grjg_zckp_fieldName);
            zckp_stb.append(" FROM ").append(zckp_tableName).append(" WHERE ").append(zcbm_zckp_fieldName).append(" = '").append(param.get("assetsCode")).append("'");
            log.info("查询资产编码查询资产卡片档案数据SQL语句：" + zckp_stb.toString());
            // 执行SQL查询并获取数据列表
            List<Object> zckp_dataList = capDataListSqlHelpManager.selectDataList(zckp_stb.toString(), null);
            FormDataMasterBean zckp_formDataMasterBean = null;
            if (!zckp_dataList.isEmpty()) {
                Map<String, Object> dataMap = (Map<String, Object>) zckp_dataList.get(0);
                bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("规格型号").getName(), dataMap.get(ggxh_zckp_fieldName));
                bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("账面价值").getName(), dataMap.get(grjg_zckp_fieldName));
                bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("账务入账日期").getName(), dataMap.get(rkrr_zckp_fieldName));
                zckp_formDataMasterBean = cap4FormManager.getDataMasterBeanById(Long.parseLong(StrKit.str(dataMap.get("id"))), zckp_formBean, null);
            }
            boolean flag = false;
            String assetsType_new = StrKit.str(param.get("assetsType"));
            String assetsLocation_new = StrKit.str(param.get("assetsLocation"));
            String assetsDeptId_new = StrKit.str(param.get("assetsDeptId"));
            String assetsUsePerson_new = StrKit.str(param.get("assetsUsePerson"));
            String assetsStatus_new = StrKit.str(param.get("assetsStatus"));
            String zclb = bmzcpd_formBean.getFieldBeanByDisplay("资产类别").getName();
            String cfwz = bmzcpd_formBean.getFieldBeanByDisplay("存放位置").getName();
            String zrbm = bmzcpd_formBean.getFieldBeanByDisplay("责任部门").getName();
            String sybgr = bmzcpd_formBean.getFieldBeanByDisplay("使用保管人").getName();
            String zczt = bmzcpd_formBean.getFieldBeanByDisplay("资产状态").getName();

            String zclb_value = StrKit.str(zckp_formDataMasterBean.getFieldValue(zckp_formBean.getFieldBeanByDisplay("资产类别").getName()));
            if (!zclb_value.equals(assetsType_new)) {
                flag = true;
            }
            String cfwz_value = StrKit.str(zckp_formDataMasterBean.getFieldValue(zckp_formBean.getFieldBeanByDisplay("存放位置").getName()));
            if (!cfwz_value.equals(assetsLocation_new)) {
                flag = true;
            }

            String zrbm_value = StrKit.str(zckp_formDataMasterBean.getFieldValue(zckp_formBean.getFieldBeanByDisplay("使用部门-W").getName()));
            if (!zrbm_value.equals(assetsDeptId_new)) {
                flag = true;
            }
            String sybgr_value = StrKit.str(zckp_formDataMasterBean.getFieldValue(zckp_formBean.getFieldBeanByDisplay("使用保管人-W").getName()));
            if (!sybgr_value.equals(assetsUsePerson_new)) {
                flag = true;
            }
            String zczt_value = StrKit.str(zckp_formDataMasterBean.getFieldValue(zckp_formBean.getFieldBeanByDisplay("资产状态").getName()));
            if (!zczt_value.equals(assetsStatus_new)) {
                flag = true;
            }

            if (flag) {
//                bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("是否修改").getName(), 5752661849850921237L);
                /*客户环境*/
                bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("是否修改").getName(), 6200186724843343106L);
            } else {
//                bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("是否修改").getName(), -6918108576882340896L);
                /*客户环境*/
                bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("是否修改").getName(), -1782616883693335803L);
            }
            bmzcpd_formDataMasterBean.addFieldValue(zclb, assetsType_new);
            bmzcpd_formDataMasterBean.addFieldValue(cfwz, assetsLocation_new);
            bmzcpd_formDataMasterBean.addFieldValue(zrbm, assetsDeptId_new);
            bmzcpd_formDataMasterBean.addFieldValue(sybgr, assetsUsePerson_new);
            bmzcpd_formDataMasterBean.addFieldValue(zczt, assetsStatus_new);
            bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("盘点人").getName(), param.get("inventoryMemberId"));
            bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("EAS编码").getName(), param.get("easCode"));
            bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("备注").getName(), param.get("remark"));
            bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("盘点日期").getName(), new Date());
            /*设置是否盘点，默认已盘点(√)*/
//            bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("是否盘到").getName(), 9219831224286202704L);
            /*客户环境*/
            bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("是否盘到").getName(), -2541467559084539290L);
            String inventoryDeptId = StrKit.str(param.get("inventoryDeptId"));
            log.info("计划盘点部门ID:" + inventoryDeptId);
            if (!assetsDeptId_new.equals(inventoryDeptId)) {
                /*(×)*/
//                bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("是否责任部门").getName(), 5164692495824537147L);
                /*客户环境*/
                bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("是否责任部门").getName(), 6672717501776152058L);
            } else {
                /*(√)*/
//                bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("是否责任部门").getName(), 9219831224286202704L);
                /*客户环境*/
                bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("是否责任部门").getName(), -2541467559084539290L);
            }

            bmzcpd_formDataMasterBean.addFieldValue(bmzcpd_formBean.getFieldBeanByDisplay("盘点情况").getName(), 7353112567001140212L);

            // 调用工具方法添加或更新表单数据
            FormCap4Kit.addUnFlowFormDataCap4(bmzcpd_formDataMasterBean, bmzcpd_formBean);
        } catch (Exception e) {
            // 日志记录异常信息
            log.error("添加或更新资产盘点情况表数据失败！", e);
            return false;
        }
        // 返回操作失败标志，实际应根据业务逻辑设置成功与否
        return true;
    }
}
